//
//  SecondHomeViewController.m
//  225视听百科
//
//  Created by 张潇 on 14/12/29.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SecondHomeViewController.h"
#import "SecondHomeTableCell.h"
#import "SecondDetailViewController.h"
#import "SettingViewController.h"
#import "HomeModel.h"
#import "MainViewController.h"
#import "ConnectModel.h"
#import "secondHomeModel.h"
#import "UIImageView+WebCache.h"
#import "MKAnnotationView+WebCache.h"
#import "ThirdHomeViewController.h"
@interface SecondHomeViewController ()

@end

@implementation SecondHomeViewController

- (void)dealloc
{
    [_secondHomeTableView release];
    [_bigView release];
    [super dealloc];
}



-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.secondHomeArray = [NSMutableArray array];

    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    UIView *white = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:white];
    
    [self startConnect];
    [self createView];
}


-(void)createView
{
    self.title = _homeModel.nickname;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nabar.jpg"] forBarMetrics:UIBarMetricsDefault];
    
    
    self.tabBarController.tabBar.hidden = YES;
    
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 25, 25);
    [rightButton setBackgroundImage:[UIImage imageNamed:@"setting.png"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(settingAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    
    self.secondHomeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _secondHomeTableView.backgroundColor = [UIColor lightGrayColor];
    _secondHomeTableView.delegate = self;
    _secondHomeTableView.dataSource = self;
    [_secondHomeTableView registerClass:[SecondHomeTableCell class] forCellReuseIdentifier:@"secondHomeCell"];
    [self.view addSubview:_secondHomeTableView];
    [_secondHomeTableView release];
    
    
    
    UIImageView *bigView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 350)];
    [bigView setImageWithURL:[NSURL URLWithString:_homeModel.mediumLogo]];
    [bigView setUserInteractionEnabled:YES];
    self.secondHomeTableView.tableHeaderView = bigView;
    
    
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeSystem];
    infoButton.frame = CGRectMake(300, 280, 50, 40);
    infoButton.backgroundColor = [UIColor orangeColor];
    [infoButton setTitle:@"简介" forState:UIControlStateNormal];
    infoButton.layer.cornerRadius = 10;
    [infoButton addTarget:self action:@selector(InfoAction:) forControlEvents:UIControlEventTouchUpInside];
    [bigView addSubview:infoButton];
    
    
    UIView *downView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 125, self.view.frame.size.width, 55)];
    downView.backgroundColor = [UIColor blackColor];
    downView.alpha = 0.9;
    [self.view addSubview:downView];
    [self.view bringSubviewToFront:downView];
    
    
   
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    returnButton.frame = CGRectMake(20, 10, 30, 30);
    [returnButton setBackgroundImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(ReturnAction:) forControlEvents:UIControlEventTouchUpInside];
    [downView addSubview:returnButton];
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_secondHomeArray count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SecondHomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondHomeCell"];
    
    cell.backgroundColor = [UIColor brownColor];
        
        secondHomeModel *secondModel = [_secondHomeArray objectAtIndex:indexPath.row];
            
        cell.nameLabel.text = secondModel.title;
        cell.countsLabel.text = [NSString stringWithFormat:@"节目数: %ld", secondModel.tracks];
        [cell.smallImg setImageWithURL:[NSURL URLWithString:secondModel.coverSmall] placeholderImage:nil];
    
    return cell;
}



-(void)settingAction:(id)sender
{
    
    SettingViewController *setting = [[SettingViewController alloc] init];
    
    [self.navigationController pushViewController:setting animated:YES];
    
    [setting release];
}



-(void)startConnect
{
    [ConnectModel connectModelWithUrlStr:[NSString stringWithFormat:@"http://app.9nali.com/777/bozhus/%ld?page_id=1&device=iPhone&version=1.1.5", (long)_homeModel.uid] parmaters:nil finished:^(NSData *data) {
       
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        self.personalSignature = [bigDic objectForKey:@"personalSignature"];
        
        NSArray *array = [bigDic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            secondHomeModel *model = [[secondHomeModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            [self.secondHomeArray addObject:model];
            [model release];
        }
        
        [_secondHomeTableView reloadData];
        
    }];
    
}


-(void)InfoAction:(id)sender
{
    SecondDetailViewController *detail = [[[SecondDetailViewController alloc] init] autorelease];
    
    [self.navigationController pushViewController:detail animated:YES];
    
    detail.detailInfo = _personalSignature;
    
//    [detail release];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        
        ThirdHomeViewController *thirdHome = [[ThirdHomeViewController alloc] init];
        
        [self.navigationController pushViewController:thirdHome animated:YES];
        
        [thirdHome release];
    
}




-(void)ReturnAction:(id)sender
{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
